using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Splino
{
    /// <summary>
    /// The form containg the Command Prompt
    /// </summary>
    public partial class CommandPrompt : Form
    {
        /// <summary>
        /// Gets the TextBox that reads the command prompt's input
        /// </summary>
        public TextBox TextBoxInput
        {
            get { return textBoxInput; }
        }

        /// <summary>
        /// Gets the TextBox that writes the command prompt's output
        /// </summary>
        public TextBox TextBoxOutput
        {
            get { return textBoxOutput; }
        }

        /// <summary>
        /// Gets the Label above the input TextBox
        /// </summary>
        public Label Label
        {
            get { return commandPromptLabel; }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public CommandPrompt()
        {
            InitializeComponent();

            commandPromptLabel.Text = CommandManager.Instance.ActiveCommandStack.Peek().Name + ": (" +
                CommandManager.Instance.ActiveCommandStack.Peek().Usage + ")";

            AutoCompleteStringCollection autoCompleteCollection = 
                new AutoCompleteStringCollection();
            autoCompleteCollection.AddRange(CommandManager.Instance.CommandList);
            textBoxInput.AutoCompleteCustomSource = autoCompleteCollection;
            textBoxInput.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            textBoxInput.AutoCompleteSource = AutoCompleteSource.CustomSource;
        }

        private void CommandPrompt_FormClosed(object sender, FormClosedEventArgs e)
        {
            CommandManager.Instance.CommandPromptActive = false;
        }

        private void textBoxInput_KeyPress(object sender, KeyPressEventArgs e)
        {
            MessageBox.Show(e.KeyChar.ToString());
        }

        private void textBoxInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && textBoxInput.Text != "")
            {
                CommandManager.Instance.ProcessCommandPromptInput(textBoxInput.Text);
                textBoxInput.Clear();
            }

        }

        private void CommandPrompt_FormClosing(object sender, FormClosingEventArgs e)
        {
            CommandManager.Instance.OutputText = textBoxOutput.Text;
        }

        private void CommandPrompt_Activated(object sender, EventArgs e)
        {
            CommandManager.Instance.ScrollTextBoxToBottom();
        }

        private void CommandPrompt_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Close();
        }

        private void textBoxInput_KeyUp(object sender, KeyEventArgs e)
        {
            CommandPrompt_KeyUp(null, e);
        }

        private void textBoxOutput_KeyUp(object sender, KeyEventArgs e)
        {
            CommandPrompt_KeyUp(null, e);
        }
    }
}